//
//  Status.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/19.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit

class Status: NSObject {
    
    //微博创建时间
    var created_at: String?
    //微博ID  NSInteger  在iphone5c 以下设备上会导致整形数据被截
    var id: Int64 = 0
    //微博信息内容
    var text: String?
    //微博来源
    var source: String?
    
    //用户
    var user:User?
    
    //配图数组
    var pic_urls:[[String:String]]?{
        didSet{
            guard let array = pic_urls else{
                return
            }
            //遍历数组
            //将数组实例化
            imageURL = [NSURL]()
            for item in array {
                //相信后端一定可以返回一个String类型的url
                var urlString = item["thumbnail_pic"]!
                urlString = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                //相信后端给定的urlString一定可以转化为一个url对象
                let url = NSURL(string: urlString)!
                //添加url
                imageURL?.append(url)
            }
        
        }
        
    }
    
    //将配图数组转化为纯种的url
    var imageURL:[NSURL]?
  
    var retweeted_status:Status?
    
    //构造方法 KVC设置值
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            // print(value)
            //字典转模型
            if let dict = value as? [String:AnyObject] {
                user = User(dict: dict)
                //print(user)
            }
            //注意 需要加return 不然字典转模型就白做了
            return
        }
        
        if key == "retweeted_status" {
        //字典转模型
            if let dict = value as? [String:AnyObject]{
            
                retweeted_status = Status(dict: dict)
                return
            }
        }
        
        
        
        
        //给其他属性赋值 (重写) 转字典
        super.setValue(value, forKey: key)
        
    }
    
    
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    //重写
    override var description:String{
        
        let key = ["created_at","id","text","source","user"]
        return dictionaryWithValuesForKeys(key).description
        
    }
    
}
